//
//  SDHSportsMatchHeaderButton.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/5.
//

#import "SDHSportsMatchHeaderButton.h"


@interface SDHSportsMatchHeaderButton () {
    UIColor *selectedColor;
    UIColor *unselectedColor;
}

@end

@implementation SDHSportsMatchHeaderButton

- (void)awakeFromNib {
    [super awakeFromNib];
    _isSelected = NO;
    self.userInteractionEnabled = YES;
    
    selectedColor = UIColorFromRGB(255, 68, 74);
    unselectedColor = UIColorFromRGB(78, 78, 78);
        
    self.backgroundColor = [UIColor redColor];
    self.lbName.numberOfLines = 0;
    self.lbName.textAlignment = NSTextAlignmentCenter;
    
    [self.btnImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.centerX.equalTo(self);
        make.height.equalTo(@30);
        make.width.equalTo(@30);
    }];
    
    [self.lbName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(5, 5, 5, 5)).priorityLow();
        make.top.equalTo(self.btnImg).offset(30);
        make.centerX.equalTo(self);
        make.width.equalTo(self.mas_width);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //设置所需的圆角位置以及大小
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)tap:(UITapGestureRecognizer *)gesture {
    self.isSelected = !_isSelected;
    if (self.btnClick) {
        self.btnClick(self.tag);
    }
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected) {
        self.backgroundColor = selectedColor;
    } else {
        self.backgroundColor = unselectedColor;
    }
}

@end
